unit BubbleSort;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

const
  N = 10;
  L = 50;
  w = 30;
  h = 30;
type
    TForm1 = class(TForm)
    Timer1: TTimer;
    Button1: TButton;
    ScrollBar1: TScrollBar;
    Button2: TButton;
    Timer2: TTimer;
    ScrollBar2: TScrollBar;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure ScrollBar2Change(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  arr: array of integer;
  panel: array of TPanel;
  i, j, t, b, s, step, runNumber, R, pan1left, pan2left, a : integer;
implementation

{$R *.dfm}

procedure Draw;
begin
  for i:=0 to n-1 do
    panel[i].Caption:=IntToStr(arr[i]);
  for b:=0 to N-1 do
  begin
    if (b=step) or (b=step+1) then
      panel[b].Color:=clGreen;
    if (b<>step) and (b<>step+1) then
      panel[b].Color:=clWhite;
    if (b>N-runNumber-1) then
    begin
      panel[b].Color:=clRed;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  R:=0;
  runNumber:=0;
  step:=0;
  setlength(arr, N);
  randomize;
  for i:=0 to N-1 do
    arr[i]:=random(L);
  Draw;
  Timer1.Enabled:=True;
end;



procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if (step<=N-2-runNumber) then
  begin
    if (arr[step]>arr[step+1]) then
    begin
      if (R=0) then
      begin
        R:=1;
        a:=0;
        pan1left:=panel[step].Left;
        pan2left:=panel[step+1].Left;
        Timer2.Enabled:=true;
        Timer1.Enabled:=false;
        exit;
      end;
      if (R=1) then
      begin
        t:=arr[step+1];
        arr[step+1]:=arr[step];
        arr[step]:=t;
        panel[step].Left:=pan1left;
        panel[step+1].Left:=pan2left;
        Draw;
        step:=step+1;
        R:=0;
      end;
    end else begin
      Draw;
      step:=step+1;
    end;
  end else begin
    runNumber:=runNumber+1;
    step:=0;
  end;                               
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  panel[step].Left:=panel[step].Left+1;
  panel[step+1].Left:=panel[step+1].Left-1;
  a:=a+1;
  Draw;
  if (a = w+20) then
  begin
    Timer1.Enabled:=true;
    Timer2.Enabled:=false;
  end;
end;

procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
  Timer1.Interval:=10*(ScrollBar1.Position)-5;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  setlength(panel, N);
  for i:=0 to N-1 do
  begin
    panel[i]:=TPanel.Create(Form1);
    panel[i].Width:=w;
    panel[i].Height:=h;
    panel[i].Left:=w*i+20*i;
    panel[i].Parent:=Form1;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if ( Timer1.Enabled=false ) then
    Timer1.Enabled:=true
  else
    Timer1.Enabled:=false;
end;


procedure TForm1.ScrollBar2Change(Sender: TObject);
begin
  Timer2.Interval:=10*(ScrollBar2.Position)-5;
end;

end.
